package com.rockville.ufone.rssdata;

//import java.net.URLDecoder;
//import java.text.SimpleDateFormat;
import java.util.*;

import org.apache.log4j.Logger;

import com.rockville.ufone.DBProc;
/**
 *
 * @author Tasnim
 */
public class SimToolKitDataUpdater implements Runnable {
	
	private static final Logger log = Logger.getLogger(SimToolKitDataUpdater.class);
    
    //@Override
    public void run() {        
        log.info("** SimToolKitTimer STARTED **");
        try {
        	DBProc db = DBProc.getInstance();        	
	        String[] simToolkitCategories = {"BOLLYWOOD", "BOLLYWOOD BUZZ", "FASHION", "GIRL IN", "GIRL OUT", "HOLLYWOOD", "HOLLYWOOD BUZZ", "JOKES", "LOLLYWOOD", "LOLLYWOOD BUZZ", "LOVE Quotes", "MEN IN", "MEN OUT", "TOP ENGLISH SONGS", "TOP URDU SONGS"};
	        int lastContentId = 0;
	        String strCat = "";
	        for (int intIndex = 0; intIndex < simToolkitCategories.length; intIndex++) {
	        	strCat = simToolkitCategories[intIndex].trim();
	        	lastContentId = db.getLatestSimToolkitContentId(strCat);
	        	Vector<String []> vec = db.fetchSimToolkitData(strCat, lastContentId, null);
	        	db.persistSimToolkitData(strCat, vec);
	        }	        
	        strCat = "BORN TODAY";
        	if(db.getLatestSimToolkitContentId(strCat) < 1) {
        		Vector<String []> vec = db.getBornToday();
        		db.persistSimToolkitData(strCat, vec);
        	}	        
        	
        	/*
        	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        	SimpleDateFormat sdf1 = new SimpleDateFormat("MMMM dd, yyyy");
        	Date lastNewsDate = db.getChannelLastUpdateDate("55");
	        String strCat = "FINANCE NEWS";
        	Vector<String []> vec = db.fetchSimToolkitData(strCat, 0, lastNewsDate);
    		for(int i=0; i<vec.size(); i++) {
    			try {
        			String [] data = vec.get(i);
        			String content = data[1];
        			content = URLDecoder.decode(content, "UTF-8");
        			if(content.indexOf("\r") != -1) {
        				content = content.substring(content.indexOf("\r")+1);
        				content = content.replaceAll("\r", "<br/>");
        			}
        			Date newsDate = sdf.parse(data[3]);        			
        			
        			db.saveNews("Headlines - "+sdf1.format(newsDate), content, newsDate, "55", null);
                    lastNewsDate = newsDate;
    			} catch(Exception e) {
    				
    			}
    		}        		
        	db.updateChannelLastNewsDate("55", lastNewsDate);
//        	"BREAKING NEWS",
//        	"LOCAL NEWS",
  */ 
 
	        
	    } catch (Exception e) {
			log.error("", e);
		}        
        log.info("------------- SimToolKitTimer Task FINISHED -------------");
    }
    
}
